<?php

namespace app\common\model\cashflow;

use think\Model;

/**
 * Company
 */
class Company extends Model
{
    // 表名
    protected $name = 'cashflow_company';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    protected $append = [
        'statics'
    ];

    public function getStaticsAttr($value, $data)
    {
        $records = \app\admin\model\cashflow\Record::
            field('cashflow_company_id,  sum(if(type = 0, income_amount, 0)) zhichu,sum(if(type = 1, income_amount, 0)) shouru')
            ->where(['cashflow_company_id' => $data['id']])
            ->group('cashflow_company_id')->find();
        $records = empty($records) ? ['zhichu' => 0, 'shouru' => 0] : $records;
        $standbys = \app\admin\model\cashflow\Standby::
        field('cashflow_company_id,  sum(if(type = 0, amount, 0)) zhichu,sum(if(type = 1, amount, 0)) shouru')
            ->where(['cashflow_company_id' => $data['id']])
            ->group('cashflow_company_id')->find();
        $standbys = empty($standbys) ? ['zhichu' => 0, 'shouru' => 0] : $standbys;
        return ['records' => $records, 'standbys' => $standbys];
    }

}